﻿using KPIS.GERP.Models;
using KPIS.GERP.Models.Class;
using KPIS.GERP.Models.FIM;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Web.Mvc;
using KPIS.GERP.Models.SYS;

namespace KPIS.GERP.WCF.GFMS
{
    [ServiceContract]
    public interface IMasBankService
    {
        [OperationContract]
        IEnumerable<SelectListItem> SelectListBank(MasBank criteriaModel = null);

        [OperationContract]
        int InsertBank(MasBank model, int createByUserId);

        [OperationContract]
        void UpdateBank(MasBank model, int updateByUserId);

        [OperationContract]
        void DeleteBank(int id, int deleteByUserId);

        [OperationContract]
        IEnumerable<KPIS.GERP.Models.FIM.MasBank> SelectBank(MasBank criteriaModel = null, List<string> includeSubObjectString = null);

        [OperationContract]
        IEnumerable<SelectListItem> SelectListBankBranch(MasBankBranch criteriaModel = null);

        [OperationContract]
        IEnumerable<KPIS.GERP.Models.FIM.MasBankBranch> SelectBankBranch(MasBankBranch criteriaModel = null, List<string> includeSubObjectString = null);

        [OperationContract]
        int InsertBankBranch(MasBankBranch model, int createByUserId);

        [OperationContract]
        void UpdateBankBranch(MasBankBranch model, int updateByUserId);

        [OperationContract]
        void DeleteBankBranch(int id, int deleteByUserId);
       
    }

    [ServiceErrorBehavior(typeof(ElmahErrorHandler))]
    public class MasBankService : IMasBankService
    {
        #region MasBank

        GERPContext db = new GERPContext();

        public IEnumerable<SelectListItem> SelectListBank(MasBank criteriaModel = null)
        {
            var list = new List<SelectListItem>();
            list.Add(new SelectListItem { Selected = true, Text = "ไม่ระบุ", Value = "" });

            try
            {
                using (var context = new GERPContext(false))
                {
                    IQueryable<MasBank> query = context.MasBanks;

                    if (criteriaModel != null)
                    {
                        object[] param;
                        query = query.Where(KPIS.GERP.Models.Class.Utility.GenerateWhereString(criteriaModel, out param), param);
                    }

                    var x = query.ToList();
                    foreach (var model in x)
                    {
                        var item = new SelectListItem { Text = model.Code + " - " + model.Name, Value = model.BankId.ToString() };
                        list.Add(item);
                    }
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }

            return list;
        }

        public IEnumerable<KPIS.GERP.Models.FIM.MasBank> SelectBank(MasBank criteriaModel = null, List<string> includeSubObjectString = null)
        {
            try
            {
                using (var context = new GERPContext(false))
                {
                    IQueryable<MasBank> query = context.MasBanks;

                    if (includeSubObjectString != null)
                    {
                        query = includeSubObjectString.Aggregate(query, (current, includePath) => current.Include(includePath));
                    }

                    if (criteriaModel != null)
                    {
                        object[] param;
                        query = query.Where(KPIS.GERP.Models.Class.Utility.GenerateWhereString(criteriaModel, out param), param);
                    }

                    return query.ToList();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int InsertBank(MasBank model, int createByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {

                    model.RecordStatus = RecordStatus.UsedStatus;
                    model.CreatedBy = createByUserId;
                    model.CreatedWhen = DateTime.Now;
                    context.MasBanks.Add(model);
                    context.SaveChanges();

                    return model.BankId;

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void UpdateBank(MasBank model, int updateByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    context.Entry(model).State = EntityState.Modified;
                    model.UpdatedBy = updateByUserId;
                    model.UpdatedWhen = DateTime.Now;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void DeleteBank(int id, int deleteByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    var modelToDelete = context.MasBanks.Find(id);
                    if (modelToDelete == null) return;

                    context.Entry(modelToDelete).State = EntityState.Modified;
                    modelToDelete.RecordStatus = RecordStatus.DeletedStatus;
                    modelToDelete.UpdatedBy = deleteByUserId;
                    modelToDelete.UpdatedWhen = DateTime.Now;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        #endregion

        #region MasBankBranch
        public IEnumerable<SelectListItem> SelectListBankBranch(MasBankBranch criteriaModel = null)
        {
            var list = new List<SelectListItem>();
            list.Add(new SelectListItem { Selected = true, Text = "ไม่ระบุ", Value = "" });

            try
            {
                using (var context = new GERPContext(false))
                {
                    IQueryable<MasBankBranch> query = context.MasBankBranchs;

                    if (criteriaModel != null)
                    {
                        object[] param;
                        query = query.Where(KPIS.GERP.Models.Class.Utility.GenerateWhereString(criteriaModel, out param), param);
                    }

                    var x = query.ToList();
                    foreach (var model in x)
                    {
                        var item = new SelectListItem { Text = model.Code + " - " + model.BranchName, Value = model.BranchId.ToString() };
                        list.Add(item);
                    }
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }

            return list;
        }

        public IEnumerable<KPIS.GERP.Models.FIM.MasBankBranch> SelectBankBranch(MasBankBranch criteriaModel = null, List<string> includeSubObjectString = null)
        {
            try
            {
                using (var context = new GERPContext(false))
                {
                    IQueryable<MasBankBranch> query = context.MasBankBranchs;

                    if (includeSubObjectString != null)
                    {
                        query = includeSubObjectString.Aggregate(query, (current, includePath) => current.Include(includePath));
                    }

                    if (criteriaModel != null)
                    {
                        object[] param;
                        query = query.Where(KPIS.GERP.Models.Class.Utility.GenerateWhereString(criteriaModel, out param), param);
                    }

                    return query.ToList();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int InsertBankBranch(MasBankBranch model, int createByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {

                    model.RecordStatus = RecordStatus.UsedStatus;
                    model.CreatedBy = createByUserId;
                    model.CreatedWhen = DateTime.Now;
                    context.MasBankBranchs.Add(model);
                    context.SaveChanges();

                    return model.BankId;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void UpdateBankBranch(MasBankBranch model, int updateByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    context.Entry(model).State = EntityState.Modified;
                    model.UpdatedBy = updateByUserId;
                    model.UpdatedWhen = DateTime.Now;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void DeleteBankBranch(int id, int deleteByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    var modelToDelete = context.MasBankBranchs.Find(id);
                    if (modelToDelete == null) return;

                    context.Entry(modelToDelete).State = EntityState.Modified;
                    modelToDelete.RecordStatus = RecordStatus.DeletedStatus;
                    modelToDelete.UpdatedBy = deleteByUserId;
                    modelToDelete.UpdatedWhen = DateTime.Now;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion
    }
}
